<?php
class CompanyModel extends CI_Model
{
    // Mongo
    const collection = 'company';
    const sequence_id = 'company';

    // Cache
    const cache_enable = true;
    const cache_ttl = 86400;
    const prefix_cache_item = 'company.%s';
    const prefix_cache_count = 'company.count.%s';
    

    public function  __construct()
    {
        parent::__construct();
    }

    public function initData($data)
    {
        // Check $data
        if(!is_array($data)) return null;

        // Define fields
        $fields = array('_id', 'name', 'category_id', 'state_id', 'city_id', 'user_id', 'params');

        $list = null;

        foreach($fields as $key) {
            if(isset($data[$key])) {
                $list[$key] = $data[$key];
            }
        }

        // Return
        return $list;
    }

    public function insert($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check data
        if(empty($data)) return false;

        // Get sequence id
        $CI = &get_instance();
        $CI->load->model('SequenceModel');
        $id = $CI->SequenceModel->getSequence(self::sequence_id);

        // Check id
        if(empty($id)) return false;

        $data['_id'] = $id;

        // Insert
        $rs = $this->mongo_db->insert(self::collection, $data);

        // Check
        if($rs) {

            // Increament id
            $CI->SequenceModel->increment(self::sequence_id);

            // Delete cache
            $cacheKey = sprintf(self::prefix_cache_item, $id);
            $this->cache->memcached->delete($cacheKey);
        }

        // Return
        return $rs;
    }

    public function update($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check Id
        if(empty($data['_id'])) return false;

        // Update mongo db
        $rs = $this->mongo_db->update(self::collection, $data);

        // Check
        if($rs) {

            // Delete cache
            $cacheKey = sprintf(self::prefix_cache_item, $data['_id']);
            $this->cache->memcached->delete($cacheKey);
        }

        // Return
        return $rs;
    }

    public function delete($companyId)
    {
        // Check company id
        if(empty($companyId)) return false;

        // Delete from mongodb
        $rs = $this->mongo_db->where(array('_id' => $companyId))->delete(self::collection);

        // Check
        if($rs) {

            // Delete cache
            $cacheKey = sprintf(self::prefix_cache_item, $companyId);
            $this->cache->memcached->delete($cacheKey);
        }

        // Return
        return $rs;
    }

    public function selectCompany($companyId)
    {
        // Check
        if(empty($companyId)) return false;

        // Init data
        $data = null;

        // Create cache key
        $cacheKey = sprintf(self::prefix_cache_item, $companyId);

        // Read cache
        if(self::cache_enable) {
            $data = $this->cache->memcached->get($cacheKey);
        }

        // Check
        if($data === false) {

            // Get from DB
            $data = $this->mongo_db->where(array('_id' => $companyId))->get(self::collection);

            // Write to cache
            $this->cache->memcached->save($cacheKey, $data, 0, self::cache_ttl);
        }

        // Return
        return $data;
        
    }

    public function selectCompanyList($where, $page)
    {
        $list = array();

        // Check
        if(empty($page) || empty($where)) return $list;

        // Set offset
        $offset = ($page - 1) * FE_COMPANY_LIMIT;

        // Select from DB
        $list = $this->mongo_db->where_and($where)->offset($offset)->limit(FE_COMPANY_LIMIT)->get(self::collection);

        // Return
        return $list;
    }

    public function selectTotalCompany($where)
    {
        // Check
        if(empty($where)) return 0;

        // Init total
        $total = 0;

        // Create cache key
        $cacheParams = null;

        // Check category
        if(isset($where['category_id'])) {
            $cacheParams[] = $where['category_id'];
        } else {
            $cacheParams[] = 0;
        }

        // Check state
        if(isset($where['state_id'])) {
            $cacheParams[] = $where['state_id'];
        } else {
            $cacheParams[] = 0;
        }

        // Check city
        if(isset($where['city_id'])) {
            $cacheParams[] = $where['city_id'];
        } else {
            $cacheParams[] = 0;
        }

        // Build cache key
        $cacheKey = sprintf(self::prefix_cache_count, implode('.', $cacheParams));

        // Read cache
        if(self::cache_enable) {
            $total = $this->cache->memcached->get($cacheKey);
        }

        // Check
        if($total === false) {

            // Get from DB
            $total = $this->mongo_db->where_and($where)->count(self::collection);

            // Write to cache
            $this->cache->memcached->save($cacheKey, $total, 0, self::cache_enable);

        }

        // Return
        return $total;
    }
}
?>